
#include "sgl_i.h"
//#include "i18n.h"
#include "smp.h"
#include "smp_menuwnd.h"
#include "smp_toolbar.h"
#include "smp_edit.h"
#include "smp_editex.h"
#include "smp_list.h"

#ifndef SDK_NO_PLUGIN
#include "sgl_string.h"
#else
#include "cookie_string.h"
#endif


LRESULT SMP_MenuWnd_WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	static DWORD sSavedIDs[SMP_TOOLBAR_ITEMCOUNT];
	HWND hControl;
	
	switch(Msg)
	{

	case WM_MODALSHOW:
	case WM_MENUSHOW:
	{
		//stop the edit blink when the focus is a edit
#if HAS_SMP_EDIT
		if(_FOCUS(hWnd) && _WNDPROC(_FOCUS(hWnd)) == SMP_EDIT_WNDPROC)
			SMP_Edit_StopBlink(_FOCUS(hWnd), FALSE);
#endif
#if HAS_SMP_EDITEX
		if(_FOCUS(hWnd) && _WNDPROC(_FOCUS(hWnd)) == SMP_EDITEX_WNDPROC)
			SMP_EditEx_StopBlink(_FOCUS(hWnd), FALSE);
#endif
#if HAS_SMP_LIST
		if(_FOCUS(hWnd) && _WNDPROC(_FOCUS(hWnd)) == SMP_LIST_WNDPROC)
			SMP_List_TitleScroll_Pause();
#endif

		if(WM_MENUSHOW == Msg)
		{
#if HAS_SMP_TOOLBAR
			//set the toolbar strings to "SELECT" and "CANCEL"
			hControl = SGL_FindChildWindowByPosition(hWnd, 0, _HEIGHT(hWnd) - SMP_TOOLBAR_HEIGHT);
			//if(!hControl && _WNDPROC(hControl) != SMP_Toolbar_WndProc)
			if(!hControl || _WNDPROC(hControl) != SMP_TOOLBAR_WNDPROC)
				return 0;
			
			sSavedIDs[SMP_TOOLBARLOC_LEFT] = SMP_Toolbar_GetString(hControl, SMP_TOOLBARLOC_LEFT);
			sSavedIDs[SMP_TOOLBARLOC_MID] = SMP_Toolbar_GetString(hControl, SMP_TOOLBARLOC_MID);
			sSavedIDs[SMP_TOOLBARLOC_RIGHT] = SMP_Toolbar_GetString(hControl, SMP_TOOLBARLOC_RIGHT);
			SMP_Toolbar_SetStrings(hControl, STR_SELECT, RESID_INVALID, STR_CANCEL, TRUE);
#endif
		}
		break;
	}

	case WM_MODALHIDE:
	case WM_MENUHIDE:
	{	
		//enable the edit blink when the focus is a edit
#if HAS_SMP_EDIT
		if(_FOCUS(hWnd) && _WNDPROC(_FOCUS(hWnd)) == SMP_EDIT_WNDPROC)
			SMP_Edit_StartBlink(_FOCUS(hWnd), FALSE);		
#endif
#if HAS_SMP_EDITEX
		if(_FOCUS(hWnd) && _WNDPROC(_FOCUS(hWnd)) == SMP_EDITEX_WNDPROC)
			SMP_EditEx_StartBlink(_FOCUS(hWnd), FALSE);		
#endif
#if HAS_SMP_LIST
		if(_FOCUS(hWnd) && _WNDPROC(_FOCUS(hWnd)) == SMP_LIST_WNDPROC)
			SMP_List_TitleScroll_Resume();
#endif

		if(WM_MENUHIDE == Msg)
		{	
#if HAS_SMP_TOOLBAR
			//restore the toolbar strings
			hControl = SGL_FindChildWindowByPosition(hWnd, 0, _HEIGHT(hWnd) - SMP_TOOLBAR_HEIGHT);
			//if(!hControl && _WNDPROC(hControl) != SMP_Toolbar_WndProc)
			if(!hControl || _WNDPROC(hControl) != SMP_TOOLBAR_WNDPROC)
				return 0;
			
			SMP_Toolbar_SetStrings(hControl, sSavedIDs[SMP_TOOLBARLOC_LEFT], sSavedIDs[SMP_TOOLBARLOC_MID], sSavedIDs[SMP_TOOLBARLOC_RIGHT], FALSE);
#endif
		}
		break;
	}

	case WM_MENUMOUSEUP:
	{
#if HAS_SMP_TOOLBAR
		if((int)lParam < _HEIGHT(hWnd) - SMP_TOOLBAR_HEIGHT)
			return 0;
		
		hControl = SGL_FindChildWindowByPosition(hWnd, 0, _HEIGHT(hWnd) - SMP_TOOLBAR_HEIGHT);
		//if(!hControl && _WNDPROC(hControl) != SMP_Toolbar_WndProc)
		if(!hControl || _WNDPROC(hControl) != SMP_TOOLBAR_WNDPROC)
			return 0;

		//the scrollbar will set as the mouse tracker on MOUSE DOWN, so we should reset the mouse tracker
		SGL_SendMessage(hControl, WM_MOUSEDOWN, wParam - _LEFT(hControl), lParam - _TOP(hControl));
		SGL_SendMessage(hControl, WM_MOUSEUP, wParam - _LEFT(hControl), lParam - _TOP(hControl));
		SGL_SetMouseTracker(NULL);
#endif
		return 1;
	}

	case WM_MENUMOUSEDOWN:
	{
		//NOT IMPLEMENTED YET
		break;
	}
	
	}

	return 0;
}

